package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import att.grappa.Node;
import taskmaster.auth.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/10/11
 * Time: 12:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class DeleteEdgeListener implements ActionListener
{

    String job;
    GraphVizGraph graphVizObject;
    User user;
    Connection con;
    EditIntFrame editIntFrame;

    public DeleteEdgeListener(String job, GraphVizGraph graphVizObject, User user, EditIntFrame editIntFrame)
    {
        this.job = job;
        this.graphVizObject = graphVizObject;
        this.user = user;
        this.con = user.getConnection();
        this.editIntFrame = editIntFrame;
    }


    public void actionPerformed(ActionEvent e)
    {
        Node[] nodes = graphVizObject.getGraph().nodeElementsAsArray();
        String[] currentNodeNames = new String[nodes.length];
        for (int i = 0; i < nodes.length; i++)
        {
            currentNodeNames[i] = nodes[i].getName();
        }
        Arrays.sort(currentNodeNames);
        JComboBox cbox1 = new JComboBox(currentNodeNames);
        JComboBox cbox2 = new JComboBox(currentNodeNames);

        Object[] message = new Object[]{"Task Tag", cbox1, "Predecessor Task", cbox2};

        int input = JOptionPane.showConfirmDialog(null, message, "Remove dependency between :", JOptionPane.OK_CANCEL_OPTION);
        if (input == JOptionPane.OK_OPTION)
        {
            String dependencyTag = cbox2.getSelectedItem().toString();
            String taskTag = cbox1.getSelectedItem().toString();
            String command = "DELETE FROM tms.task_dependencies where job_tag='" + job + "'and task_tag='" + taskTag + "'and dependency_tag='" + dependencyTag + "'";
            CallPreparedCommandSQL preparedCommandSQL = new CallPreparedCommandSQL(command, con, graphVizObject, job);

            editIntFrame.sqlCommands.add(preparedCommandSQL);
            editIntFrame.model.addElement(preparedCommandSQL.toString());
            editIntFrame.sqlcommandsList.setModel(editIntFrame.model);
        }
    }

}

